package com.nodecollege.cloud.service.impl;

import com.nodecollege.cloud.common.constants.NCConstants;
import com.nodecollege.cloud.common.exception.UpmsException;
import com.nodecollege.cloud.common.model.QueryVO;
import com.nodecollege.cloud.common.model.po.UpmsOrg;
import com.nodecollege.cloud.common.model.vo.OrgTreeVO;
import com.nodecollege.cloud.common.utils.NCUtils;
import com.nodecollege.cloud.common.utils.RedisUtils;
import com.nodecollege.cloud.dao.mapper.UpmsOrgMapper;
import com.nodecollege.cloud.service.OrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author LC
 * @date 2019/12/11 18:28
 */
@Service
public class OrgServiceImpl implements OrgService {

    @Autowired
    private UpmsOrgMapper upmsOrgMapper;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 查询组织机构树
     *
     * @param upmsOrg
     * @return
     */
    @Override
    public List<OrgTreeVO> getOrgTree(UpmsOrg upmsOrg) {
        NCUtils.nullOrEmptyThrow(upmsOrg.getTenantId(), new UpmsException("", "租户id必填！"));
        List<UpmsOrg> orgList = upmsOrgMapper.selectListByMap(new QueryVO<UpmsOrg>(upmsOrg).toMap());
        if (NCUtils.isNullOrEmpty(orgList)) {
            return new ArrayList<>();
        }
        return OrgTreeVO.getOrgTree(orgList);
    }

    /**
     * 新增组织机构
     *
     * @param upmsOrg
     */
    @Override
    public void addOrg(UpmsOrg upmsOrg) {
        NCUtils.nullOrEmptyThrow(upmsOrg.getOrgCode(), new UpmsException("-1", "组织机构代码不能为空！"));
        NCUtils.nullOrEmptyThrow(upmsOrg.getOrgName(), new UpmsException("-1", "组织机构名称不能为空！"));
        NCUtils.nullOrEmptyThrow(upmsOrg.getParentId(), new UpmsException("-1", "上级组织机构id不能为空！"));
        NCUtils.nullOrEmptyThrow(upmsOrg.getTenantId(), new UpmsException("-1", "租户id不能为空！"));

        UpmsOrg parent = upmsOrgMapper.selectByPrimaryKey(upmsOrg.getParentId());
        NCUtils.nullOrEmptyThrow(parent, new UpmsException("-1", "该上级组织机构不存在！"));

        if (!parent.getTenantId().equals(upmsOrg.getTenantId())) {
            throw new UpmsException("-1", "该上级组织机构非当前成员公司！");
        }

        UpmsOrg query = new UpmsOrg();
        query.setOrgCode(upmsOrg.getOrgCode());
        query.setTenantId(upmsOrg.getTenantId());
        List<UpmsOrg> orgList = upmsOrgMapper.selectListByMap(new QueryVO<UpmsOrg>(query).toMap());
        NCUtils.notNullOrNotEmptyThrow(orgList, new UpmsException("-1", "组织机构代码已存在！"));

        query.setOrgCode(null);
        query.setOrgName(upmsOrg.getOrgName());
        orgList = upmsOrgMapper.selectListByMap(new QueryVO<UpmsOrg>(query).toMap());
        NCUtils.notNullOrNotEmptyThrow(orgList, new UpmsException("-1", "组织机构名称已存在！"));
        upmsOrg.setState(1);
        upmsOrgMapper.insertSelective(upmsOrg);
    }

    /**
     * 更新组织机构
     *
     * @param upmsOrg
     */
    @Override
    public void updateOrg(UpmsOrg upmsOrg) {
        NCUtils.nullOrEmptyThrow(upmsOrg.getOrgId(), new UpmsException("-1", "组织机构id不能为空！"));
        UpmsOrg exit = upmsOrgMapper.selectByPrimaryKey(upmsOrg.getOrgId());
        NCUtils.nullOrEmptyThrow(exit, new UpmsException("-1", "该组织机构不存在！"));
        if (!exit.getTenantId().equals(upmsOrg.getTenantId())) {
            throw new UpmsException("-1", "该组织机构不存在！");
        }
        UpmsOrg query = new UpmsOrg();
        query.setTenantId(upmsOrg.getTenantId());
        query.setOrgName(upmsOrg.getOrgName());
        List<UpmsOrg> orgList = upmsOrgMapper.selectListByMap(new QueryVO<>(query).toMap());
        if (orgList.size() == 1 && !orgList.get(0).getOrgId().equals(upmsOrg.getOrgId())) {
            throw new UpmsException("-1", "该组织机构代码已存在！");
        }
        query.setOrgName(null);
        query.setOrgCode(upmsOrg.getOrgCode());
        orgList = upmsOrgMapper.selectListByMap(new QueryVO<>(query).toMap());
        if (orgList.size() == 1 && !orgList.get(0).getOrgId().equals(upmsOrg.getOrgId())) {
            throw new UpmsException("-1", "该组织机构名称已存在！");
        }
        upmsOrg.setState(null);
        upmsOrg.setParentId(null);
        upmsOrgMapper.updateByPrimaryKeySelective(upmsOrg);
    }

    /**
     * 删除组织机构
     *
     * @param upmsOrg
     */
    @Override
    public void delOrg(UpmsOrg upmsOrg) {
        UpmsOrg exit = upmsOrgMapper.selectByPrimaryKey(upmsOrg.getOrgId());
        NCUtils.nullOrEmptyThrow(exit, new UpmsException("-1", "该组织机构不存在！"));
        if (!exit.getTenantId().equals(upmsOrg.getTenantId())) {
            throw new UpmsException("-1", "该组织机构不存在！");
        }
        if (exit.getState().equals(NCConstants.INT_0)) {
            throw new UpmsException("-1", "该组织机构不能删除！");
        }
        UpmsOrg query = new UpmsOrg();
        query.setParentId(upmsOrg.getOrgId());
        List<UpmsOrg> orgList = upmsOrgMapper.selectListByMap(new QueryVO<>(query).toMap());
        NCUtils.notNullOrNotEmptyThrow(orgList, new UpmsException("-1", "当前组织机构存在下级组织机构，不能删除！"));
        // todo 判断该组织机构下是否还有成员 有成员不能删除
        UpmsOrg delOrg = new UpmsOrg();
        delOrg.setOrgId(upmsOrg.getOrgId());
        delOrg.setState(-1);
        upmsOrgMapper.updateByPrimaryKeySelective(delOrg);
    }

    /**
     * 查询组织机构缓存
     *
     * @param tenantId
     * @return
     */
    @Override
    public List<UpmsOrg> getOrgListCacheByTenantId(Long tenantId) {
        List<UpmsOrg> list = redisUtils.getList("upms:org:" + tenantId, UpmsOrg.class);
        if (list != null){
            return list;
        }
        UpmsOrg queryOrg = new UpmsOrg();
        queryOrg.setTenantId(tenantId);
        List<UpmsOrg> orgList = upmsOrgMapper.selectListByMap(new QueryVO<UpmsOrg>(queryOrg).toMap());
        redisUtils.set("upms:org:" + tenantId, orgList, 2 * 60 * 60);
        return orgList;
    }

    /**
     * 清除组织机构缓存
     *
     * @param tenantId
     */
    @Override
    public void clearOrgListCache(Long tenantId) {
        if (tenantId != null) {
            redisUtils.delete("upms:org:" + tenantId);
        } else {
            redisUtils.delete(redisUtils.keys("upms:org:*"));
        }
    }

    /**
     * 查询组织机构信息
     * @param upmsOrg
     * @return
     */
    @Override
    public List<UpmsOrg> getOrgList(UpmsOrg upmsOrg) {
        return upmsOrgMapper.selectListByMap(new QueryVO<>(upmsOrg).toMap());
    }
}
